<?php

namespace app\admin\controller;

use think\Controller;
use think\captcha\Captcha;
use app\admin\model\Users as M;
use app\common\model\Smssends as S;

class Logins extends Controller
{

    public function index()
    {
        return $this->fetch();
    }

    public function login_submit()
    {
        $m       = new M();
        $captcha = new Captcha();
        if (!$captcha->check(input("post.code")))
        {
            return js_return(0, '验证码错误!');
        }

        $user    = input("post.user");
        $pwd   = input("post.pwd");

        $rs = $m->where("user", $user)
//                ->where(["isLock" => 1])
            ->find();
        if (empty($rs))
            return js_return(0, '账号错误!');
        if ($rs->islock != 1)
        {
            return js_return(0, '账号已冻结!');
        }
        if ($rs->pwd == md5($pwd . md5($rs->secret_key)))
        {
            $time = date("Y-m-d H:i:s");
            $m->where('id', $rs->id)->update(['last_time' => $time]);
            cookie("member_id", $rs->id, 3600 * 24 * 90);
            session('member_info', $rs);
            return js_return(1, '登录成功!', ['url' => url("index/index/")]);
        }
        return js_return(0, '密码错误!');
    }
    //忘记密码
     public function forget()
    {
        if(request()->isPost())
        {
            $s    = new S();
            $code = input('code');
            $phone = input('phone');
            $co   = $s->getAll("code = '{$code}' and phone = '{$phone}'");

            if ($code == "")
            {
                return js_return(0, "短信验证不能为空！");
            }
            elseif (empty($co[0]['id']))
            {
                //验证码错误
                return js_return(0, "请输入正确的短信验证码！");
            }
            else
            {
                //验证码间隔时间不能大于5分钟
                $time1 = $co[0]['time'];
                $time2 = time();
                $times = $time2 - $time1;
                if ($times > 300)
                {
                    return js_return(0, "短信验证码超时，请重新获取验证码！");
                }
            }
            $m  = new M();
            $rs = $m->getAll("phone = '{$phone}'");
            if (empty($rs[0]['id']))
            {
                return js_return(0, "没有此会员");
            }
            else
            {
                cookie("forget_member_id", $rs[0]['id'], 3600);
            }
            return js_return(1, $rs[0]['kid']);
        }
        return $this->fetch();
    }
    
    public function forgettwo()
    {
        $m = new M();
        if(request()->isPost())
        {
            $s = $m->getById(cookie("forget_member_id"));
            if($s->phone != input('phone'))
            {
                return js_return(0, "数据异常！");
            }
            else
            {
                $key = get_secret_key();
                $pwd = md5(input('pwd') . md5($key));
                $opt = [
                    'pwd'=>$pwd,
                    'secret_key'=>$key,
                ];
                $m->modify($opt, $s->id);
                return js_return(1, "修改成功！");
            }
        }
        $phone = "";
        $s = $m->getByKid(input("phone"));
        if(!empty($s))
        {
            $phone = $s->phone;
        }
        $this->assign("phone",$phone);
        return $this->fetch();
    }
    
     //获取验证码
    public function get_phonecode()
    {
        $phone = input("phone");
        if ($phone == "")
        {
            return js_return(0, "手机号不能为空！");
        }
        if (!isMobile($phone))
        {
            return js_return(0, "请输入正确手机号！");
        }

        $m  = new M();
        $rs = $m->getAll("phone = '{$phone}'");
        if (empty($rs[0]['id']))
        {
            return js_return(0, "没有此会员");
        }

        //短信验证
        $s    = new S();
        $rs   = $s->getAll("phone = {$phone}");
        $code = get_code();

        //获取验证码相隔时间
        if (!empty($rs[0]['id']))
        {
            //验证码间隔时间不能小于1分钟
            $time1 = $rs[0]['time'];
            $time2 = time();
            $times = $time2 - $time1;
            if ($times < 60)
            {
                return js_return(0, "过于频繁！");
            }
            $arr = [
                'code' => $code,
                'time' => time(),
            ];
            $s->update($arr, 'id=' . $rs[0]['id']);
        }
        else
        {
            $sme = [
                'phone' => $phone,
                'code'  => $code,
                'time'  => time(),
            ];
            $s->add($sme);
        }
        $text = "您的验证码是：{$code}，5分钟内有效，请不要把验证码泄露给其他人。";
        SmsSend($phone, $text);
        return js_return(1, "短信已发送，注意查收！");
    }

}
